

***************************************************************
*1. Load qualtrics data
***************************************************************

import excel  "$data/raw/raw.xlsx",  clear firstrow case(lower)

* Destring 
destring *, replace



***************************************************************
*2. Clean qualtrics data
***************************************************************

******************
/* Demographics */
******************


gen female = (gender==2) if gender != .
drop gender
***********************************************************
/* Party membership, past campaigning, views on election */
***********************************************************

gen party_member = (member==1) if member != .
drop member

replace member_years = 0 if party_member == 0


** Campaign experience variables **
gen d_experience_campaign = (experience_campaign==1)

gen pre_doortodoor = strpos(experience_domain, "1") > 0
gen pre_posters = strpos(experience_domain, "2") > 0
gen pre_booth = strpos(experience_domain, "3") > 0
gen pre_online = strpos(experience_domain, "4") > 0
gen pre_call = strpos(experience_domain, "5") > 0
gen pre_friends = strpos(experience_domain, "6") > 0
gen pre_others =  strpos(experience_domain, "7") > 0



gen  pre_days_doortodoor = canvass_days
replace pre_days_doortodoor = 0 if pre_doortodoor == 0



** Expected vote shares ** 

* Own party
gen votes_own = voteexp_1
replace votes_own = subinstr(votes_own, ",", ".",.) 
replace votes_own = subinstr(votes_own, "%", "",.) 
replace votes_own = subinstr(votes_own, "?", "",.) 
replace votes_own = subinstr(votes_own, "$", "",.) 
replace votes_own = subinstr(votes_own, "+", "",.)
replace votes_own = subinstr(votes_own, "<", "",.) 
destring votes_own, replace

* Competition party 1
gen votes_other_1 = voteexp_2
replace votes_other_1 = subinstr(votes_other_1, ",", ".",.) 
replace votes_other_1 = subinstr(votes_other_1, "%", "",.) 
replace votes_other_1 = subinstr(votes_other_1, "?", "",.) 
replace votes_other_1 = subinstr(votes_other_1, "$", "",.) 
replace votes_other_1 = subinstr(votes_other_1, "+", "",.)
replace votes_other_1 = subinstr(votes_other_1, "<", "",.) 
destring votes_other_1, replace



* Competition party 2
gen votes_other_2 = voteexp_3
replace votes_other_2 = subinstr(votes_other_2, ",", ".",.) 
replace votes_other_2 = subinstr(votes_other_2, "%", "",.) 
replace votes_other_2 = subinstr(votes_other_2, "?", "",.) 
replace votes_other_2 = subinstr(votes_other_2, "$", "",.) 
replace votes_other_2 = subinstr(votes_other_2, "+", "",.)
replace votes_other_2 = subinstr(votes_other_2, "<", "",.) 
destring votes_other_2, replace


drop voteexp_*

**********************
/* Treatment Status */
**********************

cap gen voice_instrumental = treatment=="voice_instrumental"
label var voice_instrumental "Treatment: Instrumental, no feedback"

cap gen voice_intrinsic = treatment=="voice_intrinsic"
label var voice_intrinsic  "Treatment: Intrinsic, no feedback"


cap gen voice_feedback_intrinsic =treatment=="voice_feedback_intrinsic"
label var voice_feedback_intrinsic  "Treatment: Intrinsic, feedback"

cap gen voice_feedback_instrumental=treatment=="voice_feedback_instrumental"
label var voice_feedback_instrumental "Treatment: Instrumental, feedback"


cap gen control = treatment=="control"
label var control "Control group"

cap gen voice_instrumental_pooled = treatment=="voice_instrumental" |  treatment=="voice_feedback_instrumental"
label var voice_instrumental_pooled "Treatment: Any instrumental"
cap gen voice_intrinsic_pooled = treatment=="voice_intrinsic" | treatment=="voice_feedback_intrinsic"
label var voice_intrinsic_pooled "Treatment: Any intrinsic"

cap gen feedback_pooled = treatment=="voice_feedback_intrinsic" |  treatment=="voice_feedback_instrumental"
label var feedback_pooled "Treatment: Any feedback"
cap gen no_feedback_pooled = treatment=="voice_intrinsic" | treatment=="voice_instrumental"
label var no_feedback_pooled "Treatment: No feedback"

drop treatment
gen treat_any = control==0
label var treat_any "Any treatment"

***************************
/*  Campaign intentions  */
***************************

gen int_doortodoor = strpos(intentions, "1") > 0 if !missing(intentions)
gen int_posters = strpos(intentions, "2") > 0 if  !missing(intentions)
gen int_booth = strpos(intentions, "3") > 0  if  !missing(intentions)
gen int_online = strpos(intentions, "4") > 0 if  !missing(intentions)
gen int_call = strpos(intentions, "5") > 0  if  !missing(intentions)
gen int_friends = strpos(intentions, "6") > 0 if  !missing(intentions)
gen int_no_plans = strpos(intentions, "7") > 0  if  !missing(intentions)
gen int_others =  strpos(intentions, "8") > 0  if  !missing(intentions)

gen n_intendedact = int_doortodoor + int_posters + int_booth + int_online + int_call + int_friends if !missing(intentions) // + int_others
replace  n_intendedact =0  if int_no_plans==1 // Recode those who selected no plans as one option.

gen int_days_doortodoor = intention_days
replace int_days_doortodoor = 0 if int_doortodoor == 0



***************************
/*  Topic choices		 */
***************************

label define importance 1 "Not at all important" 2 "Not important" 3 "Important" 4 "Very important"

forval l=1/9 {
gen topic_`l'=.
foreach x in topic_voice_intr_ topic_feedback_intr_ topics_voice_instr_ topic_feedback_instr_ {
	
replace topic_`l'=`x'`l' if `x'`l' !=.	
drop `x'`l'	
}
recode topic_`l' (4=2)(6=3)(7=4)
label var topic_`l' "Importance of topic `l'"
label val topic_`l' importance
}



*************************
/* First stage beliefs */
*************************

rename manip_1 fs_engagement
rename manip_2 fs_connected
rename manip_3 fs_voice
rename manip_4 fs_interest
rename manip_5 fs_effective

label define first_stage  1 "Do not agree at all" 2 "Do not agree" 3 "Neutral" 4 "Agree" 5 "Totally agree"

foreach v of varlist fs_engagement fs_connected fs_voice fs_interest fs_effective {
lab values `v' first_stage 
recode `v' (4=1)(5=2)(6=3)(7=4)(8=5)
sum `v' if control == 1
local mean r(mean)
local sd r(sd)	

gen z_`v' = (`v'-`mean')/`sd' // Standardize outcomes

}



recode voice_prior (6=5)(7=4)(8=3)(9=2)(10=1)
label define voice_prior  1 "No, definitely not" 2 "Rather not" 3 "I am not sure" 4 "Rather yes " 5 "Yes, definitely"

lab values  voice_prior voice_prior 



gen  voice_prior_high=  inlist(voice_prior,4,5)
label var  voice_prior_high "Perceives voice in party"

foreach v of varlist voice_prior  votes_own votes_other_1 votes_other_2 {
sum `v'
replace `v'=r(mean) if missing(`v')	
	
sum `v' if control == 1
local mean r(mean)

local sd r(sd)	

gen z_`v' = (`v'-`mean')/`sd'  // Standardize controls

}


** Generate voice index **

egen z_score_fs = rowmean(z_fs_engagement z_fs_connected z_fs_voice z_fs_interest) //

sum z_score_fs if control==1

replace z_score_fs =(z_score_fs -r(mean))/r(sd)
label var z_score_fs "Voice index"
 
** Length of comment (pre-cleaned to maintain anonymity) ** 


replace comment_length=. if control==1
replace comment_length_w=. if control==1
ren comment_length_w comment_length_wins

label var comment_length  "Number of characters in feedback comment"
label var comment_length_wins  "Number of characters in feedback comment (winsorized)"

gen comment_any=comment_length_wins>0 if !missing(comment_length_wins)
label var comment_any "Left any comment"		  
		

*** Restrict sample ***	
		
keep if  progress >34 // Keep sample that saw at least one treatment screen


** Winsorize controls **
foreach x in member_years pre_days_doortodoor {

sum `x' ,d
replace `x'=r(p99) if `x' >=r(p99) &!missing(`x')
}


*** Labeling ***


lab var female "Female"

lab var party_member "Party member"
lab var member_years "Years of party membership (winsorized)" 
lab var d_experience_campaign  "Any experience campaigning" 

lab var pre_doortodoor "Experience: door canvassing"
lab var pre_days_doortodoor "Experience: \# days door canvassing (winsorized)"
lab var pre_posters "Experience: sticking poster"
lab var pre_booth "Experience: campaign booth"
lab var pre_online "Experience: social media"
lab var pre_call "Experience: phone canvassing"
lab var pre_friends "Experience: convince friends"
lab var pre_others "Experience: other"

lab var voice_prior "Perceived voice within party (1 - 5 Likert scale)"
lab var z_voice_prior "Perceived voice within party (1 - 5 Likert scale) (standardized)"
label var  voice_prior_high "Perceives voice in party"

label var votes_own "Expected vote share own party"
label var votes_other_1 "Expected vote share competitor party 1"
label var votes_other_2 "Expected vote share competitor party 2"

label var z_votes_own "Expected vote share own party (z)"
label var z_votes_other_1 "Expected vote share competitor party 1 (z)"
label var z_votes_other_2 "Expected vote share competitor party 2 (z)"

*** Outcomes ***

lab var int_no_plans "Has no plans"
lab var n_intendedact "\# intended activities"


lab var int_doortodoor "Intention: door canvassing"
lab var int_days_doortodoor "Intention: \# days door canvassing"
lab var int_posters "Intention: sticking posters"
lab var int_booth  "Intention: campaign booth"
lab var int_online  "Intention: social media"
lab var int_call "Intention: phone canvassing"
lab var int_friends  "Intention: convince friends"
lab var int_others   "Intention: other"

lab var fs_engagement "I can make a difference through my involvement in [partyname]."
lab var fs_connected "I feel connected to [partyname]."
lab var fs_voice "My opinion is being taken into account to improve the party’s election campaign."
lab var fs_interest "I have the feeling that [partyname] is interested in my opinion."
lab var fs_effective "The [partyname] has an effective campaigning strategy."


lab var z_fs_engagement "I can make a difference through my involvement in [partyname]. (standardized)"
lab var z_fs_connected "I feel connected to [partyname]. (standardized)"
lab var z_fs_voice "My opinion is being taken into account to improve the party’s election campaign. (standardized)"
lab var z_fs_interest "I have the feeling that [partyname] is interested in my opinion. (standardized)"
lab var z_fs_effective "The [partyname] has an effective campaigning strategy. (standardized)"

*** Other labels ***

label var finished "Finished survey"
label var progress "Progress in survey"
label var pid "Personal identifier"


label define treat 1 "Treatment" 0 "Control"
label values treat_any  treat


save "$data/clean/clean_main.dta" , replace

** Add comment classification **

import excel  "$data/raw/comments_raw.xlsx",  clear firstrow case(lower)
drop b

ren constructive comment_constructive
label var  comment_constructive "Constructive comment"

ren valid comment_valid
label var  comment_valid "No nonsense comment"


merge 1:1 pid using "$data/clean/clean_main.dta" 

** Recode missing values **
recode comment_constructive (.=0) if control==0

gen comment_nonsense = comment_valid==0  if control==0
label var  comment_nonsense "No nonsense comment"

drop if _merge==1
drop _merge  comment_valid

save "$data/clean/clean_main.dta" , replace
